package com.example.demo.printutil;

import android.content.Context;

import com.example.demo.MessageBean;
import com.example.demo.R;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * 测试数据生成器
 * Created by liuguirong on 8/1/17.
 */

public class PrintOrderDataMaker implements PrintDataMaker {


    private String qr;
    private int width;
    private int height;
    Context btService;
    private String remark = "微点筷客推出了餐厅管理系统，可用手机快速接单（来自客户的预订订单），进行订单管理、后厨管理等管理餐厅。";

    private MessageBean mBean;


    public PrintOrderDataMaker(Context btService, String qr, int width, int height) {
        this.qr = qr;
        this.width = width;
        this.height = height;
        this.btService = btService;
    }

    public void setMessage(String msg) {
        Gson gson = new Gson();
        mBean = gson.fromJson(msg, MessageBean.class);
    }

    @Override
    public List<byte[]> getPrintData(int type) {
        ArrayList<byte[]> data = new ArrayList<>();

        if (mBean == null) {
            return null;
        }

        try {
            PrinterWriter printer;
            switch (type) {
                case PrinterWriter58mm.TYPE_58:
                    printer = new PrinterWriter58mm(height, width);
                    break;
                case PrinterWriter80mm.TYPE_80:
                    printer = new PrinterWriter80mm(height, width);
                    break;
                case PrinterWriter55mm.TYPE_55:
                default:
                    printer = new PrinterWriter55mm(height, width);
                    break;
            }
            //printer = type == PrinterWriter58mm.TYPE_58 ? new PrinterWriter58mm(height, width) : new PrinterWriter80mm(height, width);
            printer.setAlignCenter();
            data.add(printer.getDataAndReset());

            ArrayList<byte[]> image1 = printer.getImageByte(btService.getResources(), R.drawable.demo);

            data.addAll(image1);

            printer.setAlignLeft();
            printer.printLine();
            printer.printLineFeed();

            printer.printLineFeed();
            printer.setAlignCenter();
            printer.setEmphasizedOn();
            printer.setFontSize(1);
            printer.print("欢迎光临本店");
            printer.printLineFeed();
            printer.setEmphasizedOff();
            printer.printLineFeed();
            printer.printLine();

            printer.printLineFeed();
            printer.setFontSize(0);
            printer.setAlignCenter();
            printer.print("Num：" + mBean.getId());
            printer.printLineFeed();

            printer.setAlignCenter();
            printer.print(mBean.getTime());
            printer.printLineFeed();

            printer.printLine();

//            printer.printLineFeed();
//            printer.setAlignLeft();
//            printer.print("订单状态: " + "已接单");
//            printer.printLineFeed();
//            printer.print("用户昵称: " +"周末先生");
//            printer.printLineFeed();
//            printer.print("用餐人数: " + "10人");
//            printer.printLineFeed();
//            printer.print("用餐桌号:" + "A3" + "号桌");
//            printer.printLineFeed();
//            printer.print("预定时间：" + "2017-10-1 17：00");
//            printer.printLineFeed();
//            printer.print("预留时间：30分钟");
//            printer.printLineFeed();
//            printer.print("联系方式：" + "18094111545454");
//            printer.printLineFeed();
//            printer.printLine();
//            printer.printLineFeed();
//
//            printer.setAlignLeft();
//            printer.print("备注：" + "记得留位置");
//            printer.printLineFeed();
//            printer.printLine();
//
//            printer.printLineFeed();

            printer.setAlignCenter();
//            printer.print("菜品信息");
//            printer.printLineFeed();
//            printer.setAlignCenter();
            printer.printInOneLine("菜品名称", "英文名称", "数量", "金额", 0);
            printer.printLineFeed();

            for (Map<String, String> bill : mBean.getList()) {
                printer.printInOneLine(bill.get("p_pro"), bill.get("p_en_pro"), bill.get("p_result"), bill.get("p_sum"), 0);
                printer.printLineFeed();
            }

            printer.printLineFeed();
            printer.printLine();
            printer.printLineFeed();
            printer.setAlignLeft();
            printer.printInOneLine("总计：", mBean.getTotal() + "", 0);

            printer.setAlignLeft();
            printer.printInOneLine("优惠后费用：", mBean.getOff() + "", 0);
            printer.printLineFeed();

            printer.printLine();

            printer.setAlignLeft();
            printer.print("Address： xxxxxxxx");
            printer.printLineFeed();


            printer.setAlignLeft();
            printer.print("Tel： 15822671581");
            printer.printLineFeed();

            printer.printLine();
            printer.printLineFeed();
            printer.setAlignCenter();
            printer.print("谢谢惠顾，欢迎再次光临！");
            printer.printLineFeed();
            printer.printLineFeed();
            printer.printLineFeed();
            printer.feedPaperCutPartial();

            data.add(printer.getDataAndClose());
            return data;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }


}
